package org.example.springboot.controller;



import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import org.example.springboot.common.Result;
import org.example.springboot.entity.IconItem;

import org.example.springboot.mapper.IconItemMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static org.example.springboot.common.Result.error;
import static org.example.springboot.common.Result.success;


@RestController
@RequestMapping("/icon-item")
public class IconItemController {
    private static final Logger LOGGER = LoggerFactory.getLogger(IconItemController.class);
    @Autowired
    private IconItemMapper iconItemMapper;



    /**
     * 新增/修改字典项
     * @param iconItem
     * @return
     */
    @PostMapping("/save")
    @Operation(summary = "新增/修改字典项")
    public Result<?> save(@RequestBody IconItem iconItem) {
        QueryWrapper<IconItem> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("item_key", iconItem.getItemKey());

        List<Object> exitDictItem = iconItemMapper.selectObjs(queryWrapper);
        if(!exitDictItem.isEmpty()){
            return error("-1","该项目已存在");
        }
        boolean result = iconItemMapper.insert(iconItem)>0;

        return result? success() : error("-1", "保存失败");
    }


    @Operation(summary = "更新字典项")
    @PutMapping("/{id}")
    public Result<?>  update(@PathVariable int id, @RequestBody IconItem iconItem) {
        iconItem.setId(id);
        LOGGER.info("UPDATE dictItem:"+ iconItem);
        int res = iconItemMapper.updateById(iconItem);
        if(res>0){
            return success(iconItem);
        }else{
            return error("-1","修改失败");
        }
    }

    @GetMapping("/all")
    @Operation(summary = "查询所有图标")
    public Result<?> getByTypeCode() {
        List<IconItem> itemList = iconItemMapper.selectList(null);
        LOGGER.info("LOADING itemList:"+itemList);
        return success(itemList);
    }
    /**
     * 根据ID删除字典项
     * @param id
     * @return
     */
    @DeleteMapping("/deleteById/{id}")
    @Operation(summary = "根据ID删除字典项")
    public Result<?> deleteById(@PathVariable Integer id) {
        boolean result = iconItemMapper.deleteById(id) > 0;
        return result ? success() : error("-1", "删除失败");
    }


    /**
     * 批量删除字典项
     * @param idList
     * @return
     */
    @DeleteMapping("/deleteBatch")
    @Operation(summary = "批量删除字典项")
    public Result<?> deleteBatch(@RequestParam List<Integer> idList) {
        boolean result = iconItemMapper.deleteByIds(idList) > 0;
        return result ? success() : error("-1", "删除失败");
    }



    /**
     * 分页查询字典项
     * @param currentPage
     * @param size
     * @param itemKey
     * @return
     */
    @GetMapping("/findPage")
    @Operation(summary = "分页查询字典项")
    public Result<?> findPage(@RequestParam Integer currentPage,
                              @RequestParam Integer size,
                              @RequestParam(name = "itemKey", defaultValue = "") String itemKey) {
        Page<IconItem> page = new Page<>(currentPage, size);
        IconItem iconItem = new IconItem();

        iconItem.setItemKey(itemKey);
        QueryWrapper<IconItem> queryWrapper = new QueryWrapper<>();

        if (!itemKey.isEmpty()) {
            queryWrapper.like("item_key", iconItem.getItemKey());
        }
        Page<IconItem> dictItemPage = iconItemMapper.selectPage(page, queryWrapper);
        return success(dictItemPage);
    }
}